package com.caseProcess.utils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author duzq
 * @date 2020/11/18 9:41
 **/
public class TimeUtil {

    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    /**
     * 获取系统当前日期时间 年月日
     *
     * @return 系统当前日期时间
     */
    public static String getNowTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(System.currentTimeMillis());
    }

    /**
     * 获得指定日期的后一天
     *
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
            return getSpecifiedDayAfter(getNowTime());
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);
        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }

    /**
     * 获取当前时间前size dateType的日子
     *
     * @param dateType 类型 0 日 ,1 月 2 年,3 星期
     * @param size     相差
     * @return
     */
    public static String getNowDetailedTime(int dateType, int size) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String dateTime = "";
        switch (dateType) {
            case 0:
                c.setTime(new Date());
                c.add(Calendar.DATE, -size);
                Date d = c.getTime();
                dateTime = format.format(d);
                break;
            case 1:
                c.setTime(new Date());
                c.add(Calendar.MONTH, -size);
                Date m = c.getTime();
                dateTime = format.format(m);
                break;
            case 2:
                c.setTime(new Date());
                c.add(Calendar.YEAR, -size);
                Date y = c.getTime();
                dateTime = format.format(y);
                break;
            case 3:
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, -size);
                Date month = c.getTime();
                dateTime = format.format(month);
                break;
            default:
                c.setTime(new Date());
                dateTime = format.format(c.getTime());
                break;
        }
        return dateTime;
    }

    //判断 并转换时间格式
    public static String getTime(String ditNumber) {
        //如果不是数字
        if (!isNumeric(ditNumber)) {
            return null;
        }
        //如果是数字 小于0则 返回
        BigDecimal bd = new BigDecimal(ditNumber);
        int days = bd.intValue();//天数
        int mills = (int) Math.round(bd.subtract(new BigDecimal(days)).doubleValue() * 24 * 3600);

        //获取时间
        Calendar c = Calendar.getInstance();
        c.set(1900, 0, 1);
        c.add(Calendar.DATE, days - 2);
        int hour = mills / 3600;
        int minute = (mills - hour * 3600) / 60;
        int second = mills - hour * 3600 - minute * 60;
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);

        return dateFormat.format(c.getTime());
    }

    public static void main(String[] args) {
        System.out.println(getTime("44022.9782638889"));
    }

    //校验是否数据含小数点
    private static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]+\\.*[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}
